package com.css.fxfzypg.modules.suggestinfo.manage.disasterscenarioManage.service.impl;

import com.css.fxfzypg.modules.suggestinfo.analysis.suggestionanalysis.vo.ParamsVo;
import com.css.fxfzypg.modules.suggestinfo.manage.disasterscenarioManage.repository.DisasterScenarioManageRepository;
import com.css.fxfzypg.modules.suggestinfo.manage.disasterscenarioManage.repository.entity.DisasterScenarioManageEntity;
import com.css.fxfzypg.modules.suggestinfo.manage.disasterscenarioManage.service.DisasterScenarioManageService;
import com.css.fxfzypg.modules.suggestinfo.manage.vo.ParamVo;
import com.css.fxfzypg.util.CreateGson;
import com.css.fxfzypg.util.PlatformObjectUtils;
import com.css.fxfzypg.util.UUIDGenerator;
import com.css.fxfzypg.web.PlatformSessionContext;
import com.css.fxfzypg.zcpt.sys.entity.SUser;
import com.css.fxfzypg.zcpt.sys.service.SUserService;
import com.css.fxfzypg.zcpt.sys.service.SysAreaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class DisasterScenarioManageServiceImpl implements DisasterScenarioManageService {

    @Autowired
    DisasterScenarioManageRepository disasterScenarioManageRepository;
    @Autowired
    SysAreaService sysAreaService;
    @Autowired
    SUserService sUserService;


    @Override
    public Map<String, Object> save(DisasterScenarioManageEntity disasterScenarioManageEntity) throws UnsupportedEncodingException {
        Map<String, Object> map = new HashMap<>();
        String provinceId = disasterScenarioManageEntity.getProvinceId();
        String cityId = disasterScenarioManageEntity.getCityId();
        String areaId = disasterScenarioManageEntity.getAreaId();
        //检验是否有重复的区县建议
        boolean res = verifySuggestion("0", disasterScenarioManageEntity.getId(), provinceId, cityId, areaId);
        if (!res) {
            map.put("res", res);
            map.put("mess", "该省市区县已存在,不能添加!");
            return map;
        }
        disasterScenarioManageEntity.setId(UUIDGenerator.getUUID());
        disasterScenarioManageEntity.setProvince(sysAreaService.getAreaNameByAreaId(URLEncoder.encode(provinceId, "UTF-8")));
        disasterScenarioManageEntity.setCity(sysAreaService.getAreaNameByAreaId(URLEncoder.encode(cityId, "UTF-8")));
        if(PlatformObjectUtils.isNotEmpty(areaId)){
            disasterScenarioManageEntity.setArea(sysAreaService.getAreaNameByAreaId(URLEncoder.encode(areaId, "UTF-8")));
        }
        disasterScenarioManageEntity.setDelFlag("0");
        String userId = PlatformSessionContext.getUserID();
        SUser sUser = CreateGson.createGson().fromJson(sUserService.getUserById(userId), SUser.class);
        disasterScenarioManageEntity.setCreateId(sUser.getUserId());
        disasterScenarioManageEntity.setCreateTime(new Date());
        boolean save = disasterScenarioManageRepository.save(disasterScenarioManageEntity);
        map.put("res", save);
        return map;
    }

    @Override
    public Map<String, Object> getDataList(ParamVo paramVo, int curPage, int pageSize) {
        return disasterScenarioManageRepository.getDataList(paramVo, curPage, pageSize);
    }

    @Override
    public DisasterScenarioManageEntity getDataById(String id) {
        return disasterScenarioManageRepository.getDataById(id);
    }

    @Override
    public Map<String, Object> update(DisasterScenarioManageEntity disasterScenarioManageEntity) throws UnsupportedEncodingException {
        Map<String, Object> map = new HashMap<>();
        //检验是否有重复的区县建议
        boolean res = verifySuggestion("1", disasterScenarioManageEntity.getId(), disasterScenarioManageEntity.getProvinceId(), disasterScenarioManageEntity.getCityId(), disasterScenarioManageEntity.getAreaId());
        if (!res) {
            map.put("res", res);
            map.put("mess", "该省市区县已存在,不能修改!");
            return map;
        }
        disasterScenarioManageEntity.setProvince(sysAreaService.getAreaNameByAreaId(URLEncoder.encode(disasterScenarioManageEntity.getProvinceId(), "UTF-8")));
        disasterScenarioManageEntity.setCity(sysAreaService.getAreaNameByAreaId(URLEncoder.encode(disasterScenarioManageEntity.getCityId(), "UTF-8")));
        disasterScenarioManageEntity.setArea(sysAreaService.getAreaNameByAreaId(URLEncoder.encode(disasterScenarioManageEntity.getAreaId(), "UTF-8")));
        String userId = PlatformSessionContext.getUserID();
        SUser sUser = CreateGson.createGson().fromJson(sUserService.getUserById(userId), SUser.class);
        disasterScenarioManageEntity.setUpdateId(sUser.getUserId());
        disasterScenarioManageEntity.setUpdateTime(new Date());
        boolean update = disasterScenarioManageRepository.update(disasterScenarioManageEntity);
        map.put("res", update);
        return map;
    }

    @Override
    public boolean delete(List<String> idList) {
        boolean res = false;
        for (String id : idList) {
            res = disasterScenarioManageRepository.delete(id);
            if (!res) {
                break;
            }
        }
        return res;
    }

    @Override
    public DisasterScenarioManageEntity getDisasterScenarioManageData(ParamsVo paramsVo) {
        DisasterScenarioManageEntity disasterScenarioManageData = null;
        if (paramsVo.getFlag().equals("0")) {
            disasterScenarioManageData = disasterScenarioManageRepository.getDisasterScenarioManageData(paramsVo);
        }
        return disasterScenarioManageData;
    }

    @Override
    public List<DisasterScenarioManageEntity> getDisasterScenarioManageAreaData(ParamsVo paramsVo) {
        return disasterScenarioManageRepository.getDisasterScenarioManageAreaData(paramsVo);
    }

    @Override
    public DisasterScenarioManageEntity getByProvinceCityCounty(String provinceId, String cityId, String areaId) {
        return disasterScenarioManageRepository.getByProvinceCityCounty(provinceId, cityId, areaId);
    }

    /**
     * 校验区县是否存在
     *
     * @param flag       0:保存  1:修改
     * @param provinceId
     * @param id
     * @param cityId
     * @param areaId
     * @return
     */
    private boolean verifySuggestion(String flag, String id, String provinceId, String cityId, String areaId) {
        boolean res = false;
        int count = disasterScenarioManageRepository.getCount(provinceId, cityId, areaId);
        if (flag.equals("0")) {
            if (count == 0) {
                res = true;
            }
        } else if (flag.equals("1")) {
            if (count == 0) {
                res = true;
            } else {
                String uuid = disasterScenarioManageRepository.getId(provinceId, cityId, areaId);
                if (uuid.equals(id)) {
                    res = true;
                } else {
                    res = false;
                }
            }
        }
        return res;
    }
}
